/**
* Copyright 2009, Jethro Grassie, Creative Jar Technology Ltd (http://www.cjtech.co.uk)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
**/

package com.adobe.cairngorm.control;

import java.util.HashMap;

public class CairngormEventDispatcher
{
    protected var listeners:HashMap = new HashMap();

    public function addEventListener(type:String, listener:function(event:CairngormEvent):Void):Void 
    {
        listeners.put(type, listener);
    }

    public function removeEventListener(type:String, listener:function(event:CairngormEvent):Void):Void 
    {
        listeners.remove(type);
    }

    public function dispatchEvent(event:CairngormEvent):Boolean 
    {
        if(not listeners.containsKey(event.type))
            return false;
        var f = listeners.get(event.type) as function(event:CairngormEvent):Void;
        f(event);
        return true;
    }

    public function hasEventListener(type:String):Boolean 
    {
        return listeners.containsKey(type);
    }

    public function willTrigger(type:String):Boolean 
    {
        return listeners.containsKey(type);
    }
}

var instance:CairngormEventDispatcher = CairngormEventDispatcher{};
public function getInstance():CairngormEventDispatcher 
{
    return instance;
}

